package org.havenapp.main;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import org.havenapp.main.service.MonitorService;
import org.havenapp.main.ui.AccelConfigureActivity;
import org.havenapp.main.ui.CameraConfigureActivity;
import org.havenapp.main.ui.MicrophoneConfigureActivity;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class MainActivity extends AppCompatActivity {

    private RecyclerView rvLog;
    private Button btnStart;

    private PreferenceManager manager;
    private MAdapter mAdapter;

    private Context mContext;
    private android.support.v7.widget.Toolbar toolbar;

    private boolean mIsMonitoring;
    private String log = "";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mContext = this;
        initView();
        initData();
    }


    private void initView() {
        rvLog = findViewById(R.id.rv_log);
        btnStart = findViewById(R.id.btn_start);
        toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        btnStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(mContext, MonitorActivity.class);
                startActivity(intent);
            }
        });
    }


    private void initData() {
        manager = new PreferenceManager(this);
//        manager = new PreferenceManager(this);
//        log = manager.getLog();
//
//        initLog();
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                try {
//                    Thread.sleep(1000);
//                    String logNew = manager.getLog();
//                    Log.e("buxq", "run: logNew = " + logNew);
//                    if (TextUtils.isEmpty(logNew)) {
//                        return;
//                    }
//                    if (!logNew.equals(log)) {
//                        toolbar.post(new Runnable() {
//                            @Override
//                            public void run() {
//                                initLog();
//                            }
//                        });
//                    }
//                } catch (InterruptedException e) {
//
//                    e.printStackTrace();
//                }
//
//            }
//        }).start();
    }

    private void initLog() {
        if (!TextUtils.isEmpty(log)) {
            String[] split = log.split("###");
            if (split != null && split.length > 0) {
                mAdapter = new MAdapter(split);
                rvLog.setAdapter(mAdapter);
                LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
                linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
                rvLog.setLayoutManager(linearLayoutManager);
                mAdapter.notifyDataSetChanged();
                if (rvLog.getVisibility() != View.VISIBLE) {
                    rvLog.setVisibility(View.VISIBLE);
                }
            }
        }
    }

    private void initMonitor() {

        mIsMonitoring = true;
        //ensure folder exists and will not be scanned by the gallery app

        try {
            File fileImageDir = new File(Environment.getExternalStorageDirectory(), manager.getImagePath());
            fileImageDir.mkdirs();
            new FileOutputStream(new File(fileImageDir, ".nomedia")).write(0);
        } catch (IOException e) {
            Log.e("Monitor", "unable to init media storage directory", e);
        }

        //Do something after 100ms
        startService(new Intent(MainActivity.this, MonitorService.class));
    }

    private void stopMonitor() {
        mIsMonitoring = false;
        stopService(new Intent(this, MonitorService.class));
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main_now, menu);
        return true;
    }



    class MAdapter extends RecyclerView.Adapter<LogHolder> {

        private String[] logs;

        public MAdapter(String[] logs) {
            this.logs = logs;
        }

        @NonNull
        @Override
        public LogHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_log, parent, false);
            LogHolder holder = new LogHolder(view);
            return holder;
        }

        @Override
        public void onBindViewHolder(@NonNull LogHolder holder, int position) {
            holder.tvLog.setText(logs[position]);
        }

        @Override
        public int getItemCount() {
            return logs.length;
        }
    }

    static class LogHolder extends RecyclerView.ViewHolder {
        TextView tvLog;

        public LogHolder(View view) {
            super(view);
            tvLog = view.findViewById(R.id.tv_log);
        }
    }
}
